package com.zzs.server.controller;

import com.zzs.common.core.response.Result;
import com.zzs.server.pojo.bo.code.SaveProblemCodeBO;
import com.zzs.server.pojo.bo.code.UpdateProblemCodeBO;
import com.zzs.server.pojo.dto.code.SaveProblemCodeDTO;
import com.zzs.server.pojo.dto.code.UpdateProblemCodeDTO;
import com.zzs.server.pojo.query.ProblemCodeQuery;
import com.zzs.server.pojo.vo.ProblemCodeDetailVO;
import com.zzs.server.service.ProblemCodeService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author zzs
 * @date 2025/03/18
 */
@RestController
@RequestMapping("/server/code")
public class ProblemCodeController {
    @Autowired
    private ProblemCodeService problemCodeService;

    @PostMapping
    public Result<Void> saveProblemCode(@Valid @RequestBody SaveProblemCodeDTO saveProblemCodeDTO) {
        LocalDateTime now = LocalDateTime.now();
        SaveProblemCodeBO saveProblemCodeBO = SaveProblemCodeBO.builder()
                .problemId(saveProblemCodeDTO.getProblemId())
                .language(saveProblemCodeDTO.getLanguage())
                .mainFunction(saveProblemCodeDTO.getMainFunction())
                .userFunction(saveProblemCodeDTO.getUserFunction())
                .createTime(now)
                .updateTime(now)
                .build();
        problemCodeService.saveProblemCode(saveProblemCodeBO);
        return Result.success();
    }

    @PutMapping
    public Result<Void> updateProblemCode(@Valid @RequestBody UpdateProblemCodeDTO updateProblemCodeDTO) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(updateProblemCodeDTO.getMainFunction().length());
        UpdateProblemCodeBO updateProblemCodeBO = UpdateProblemCodeBO.builder()
                .problemId(updateProblemCodeDTO.getProblemId())
                .language(updateProblemCodeDTO.getLanguage())
                .mainFunction(updateProblemCodeDTO.getMainFunction())
                .userFunction(updateProblemCodeDTO.getUserFunction())
                .updateTime(now)
                .build();
        problemCodeService.updateProblemCode(updateProblemCodeBO);
        return Result.success();
    }

    @GetMapping
    public Result<ProblemCodeDetailVO> getProblemCode(ProblemCodeQuery problemCodeQuery) {
        ProblemCodeDetailVO problemCodeDetailVO = problemCodeService.getProblemCode(problemCodeQuery.getProblemId(), problemCodeQuery.getLanguage());
        return Result.success(problemCodeDetailVO);
    }

    @DeleteMapping
    public Result<Void> removeProblemCode(ProblemCodeQuery problemCodeQuery) {
        problemCodeService.removeProblemCode(problemCodeQuery.getProblemId(), problemCodeQuery.getLanguage());
        return Result.success();
    }
}
